package com.servlets;

import com.alibaba.fastjson.JSON;
import com.pojo.Brand;
import com.pojo.CarouselFigure;
import com.pojo.PageBean;
import com.service.BrandService;
import com.service.impl.BrandSercviceImpl;
import com.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/brand.do")
public class BrandServlet extends BaseServlet{
    private BrandService service = new BrandSercviceImpl();

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String current = request.getParameter("current");
        String limit = request.getParameter("limit");
        Integer curr=1,lim = 10;
        if (current!=null)
            curr=Integer.parseInt(current);
        if (limit!=null)
            lim = Integer.parseInt(limit);
        PageBean<Brand> page = service.findAll(lim, curr);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JDBCUtil.toLayuiJson(page.getTotal(),page.getData()));

    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        Brand brand = JDBCUtil.mapToBean(map, Brand.class);
        brand.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
        String msg ="";
        if(service.addBrand(brand))
            msg="成功";
        else
            msg = "失败";
        response.setContentType("application/json;charset=utf-8;");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void findByID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        Brand br = service.findByID(id);
        response.setContentType("application/json;charset=utf-8;");
        response.getWriter().write(JSON.toJSONString(br));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id"),msg;
        if(service.deleteBrand(id))
            msg = "删除成功";
        else
            msg = "删除失败";
        response.setContentType("application/json;charset=utf-8;");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        Brand brand = JDBCUtil.mapToBean(map, Brand.class);
        System.out.println(brand);
        String msg ="";
        if (service.updateBrand(brand))
            msg="成功";
        else
            msg = "失败";
        response.setContentType("application/json;charset=utf-8;");
        response.getWriter().write(JSON.toJSONString(msg));

    }

    public void findByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String name = request.getParameter("name");
        boolean bool=false;
        if (service.findByName(name)==null){
            bool=true;
        }
        response.setContentType("application/json;charset=utf-8;");
        response.getWriter().write(JSON.toJSONString(bool));
    }

    public void findByBrandType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String productTypeName = request.getParameter("productTypeName");
        List<Brand> list = service.findByBrandType(productTypeName);
        response.setContentType("application/json;charset=utf-8;");
        response.getWriter().write(JSON.toJSONString(list));
    }
}
